/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on 15.08.2007
 * @author Asia Grzibovska
 *
 */

package uk.ac.sanger;

import org.xml.sax.*;             // The main SAX package

import java.io.IOException;
//import java.lang.reflect.Method;
//import java.net.ConnectException;
//import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

public class UpdateCoordinate {

	public static final String REGISTRY_LOCATION = 
		"http://www.dasregistry.org/das1/coordinatesystem";
	
	public void makeUpdateCoordinates(){
		try{
			//call XML parser
			String[][] coordinates ;
			//get list of coordinates from XML
			coordinates = handleParser(REGISTRY_LOCATION);

			//for(int i=0;i<coordinates.length;i++){

			//	System.out.println("makeUpdateCoordinates "+coordinates[i][0]);
			//	System.out.println("makeUpdateCoordinates "+coordinates[i][1]);
			//}

			//add missing coordinate system to db
			DataManager dbM = new DataManager();
			dbM.saveCoordinate(coordinates);	

		}
		catch (Exception err){
			err.printStackTrace();
		}
	}
	
	public String[][] handleParser(String REGISTRY_LOCATION) throws IOException, SAXException {

		// Create the parser we'll use.  The parser implementation is a 
		// Xerces class, but we use it only through the SAX XMLReader API
		org.xml.sax.XMLReader parser=new org.apache.xerces.parsers.SAXParser();

		// Specify that we don't want validation.  This is the SAX2
		// API for requesting parser features.  Note the use of a
		// globally unique URL as the feature name.  Non-validation is
		// actually the default, so this line isn't really necessary.
		parser.setFeature("http://xml.org/sax/features/validation", false);

		// Instantiate this class to provide handlers for the parser and 
		// tell the parser about the handlers
		CoordinateXMLParser handler = new CoordinateXMLParser();
		parser.setContentHandler(handler);
		parser.setErrorHandler(handler);

		// Create an input source that describes the file to parse.
		// Then tell the parser to parse input from that source
		//org.xml.sax.InputSource input=new InputSource(new FileReader("/nfs/team71/phd/ag7/workspace/writebackServlet/coordinatesystem.xml"));
		try {
			URL u = new URL(REGISTRY_LOCATION);
			URLConnection huc = u.openConnection();
			//URLConnection huc =  openHttpURLConnection(u, 15);			
			InputSource input = new InputSource(huc.getInputStream());
			parser.parse(input);

		} catch (Exception e){
			e.printStackTrace();
		}
		return handler.getCoordinates();
	}
	
	/*public static HttpURLConnection openHttpURLConnection(URL url, int timeout)
	throws IOException, ConnectException{
		
		
		System.out.println("opening HTTP connection to " + url + " " + System.getProperty("proxyHost"));
		
		System.setProperty("proxySet", "true");
        System.setProperty("proxyHost", "wwwcache.sanger.ac.uk");
        System.setProperty("proxyPort", "3128");
		
		
		HttpURLConnection huc = null;

		huc = (HttpURLConnection) url.openConnection();
		huc.addRequestProperty("User-Agent", "DAS_WRITEBACK_SERVER");

		// this sets the timeouts for Java 1.4
		System.setProperty("sun.net.client.defaultConnectTimeout", ""+timeout);
		System.setProperty("sun.net.client.defaultReadTimeout", ""+timeout);

		// for Java 1.5 we need to do this:
		// use reflection to determine if get and set timeout methods for urlconnection are available
		// seems java 1.5 does not watch the System properties any longer...
		// and java 1.4 did not provide the new classes...

		try {
			// try to use reflection to set timeout property
			Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");

			Method setconnecttimeout = urlconnectionClass.getMethod (
					"setConnectTimeout", new Class [] {int.class}        
			); 
			setconnecttimeout.invoke(huc,new Object[] {new Integer(timeout)});

			Method setreadtimeout = urlconnectionClass.getMethod (
					"setReadTimeout", new Class[] {int.class}
			);
			setreadtimeout.invoke(huc,new Object[] {new Integer(timeout)});
			
			String proxySet = System.getProperty("proxySet");
			
			if ( proxySet != null && (proxySet.equals("true"))){
				System.out.println("proxy has been set");
				
				int proxyPort = Integer.parseInt(System.getProperty("proxyHost"));
				SocketAddress addr = new
				
				InetSocketAddress(System.getProperty("proxyHost"), proxyPort);
				Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
				System.out.println(proxy);
				
				System.out.println(System.getProperty("proxyHost"));
				System.out.println(System.getProperty("proxyPort"));
			}
			
			
			
			
			System.out.println("successfully set java 1.5 timeout");
		} catch (Exception e) {
			e.printStackTrace();
			// most likely it was a NoSuchMEthodException and we are running java 1.4.
		}
		return huc;
	}	
*/
}
